<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h2>混入</h2>
      <p>count: {{ count }}</p>
      <child></child>
    </div>

    <script src="./lib/Vue2.js"></script>
    <script>
      // 混入：功能分发
      // 从组件里面去将相同的内容提取出来

      // 创建混入对象
      // 混入对象里面能写的东西和组件几乎相同，除了没有template
      const myMixin = {
        data() {
          return {
            count: 10,
          };
        },
        methods: {
          fn() {
            console.log("fn");
          },
        },
        created() {
          console.log("mixin created");
        },
      };

      const myMixin2 = {
        data() {
          return {
            name: "zhangsan",
          };
        },
      };

      const Child = {
        // mixins是将混入对象进行混入
        // 对象，可以混入多个
        mixins: [myMixin, myMixin2],
        data() {
          return {
            age: 20,
            name: "lisi",
          };
        },
        template: `
          <div>child子组件 - {{ count }}</div>
        `,
        created() {
          console.log("child created");
        },
        mounted() {
          this.fn();
        },
      };

      new Vue({
        el: "#app",
        mixins: [myMixin],
        components: {
          Child,
        },
      });
    </script>
  </body>
  <html></html>
</html>
